package ua.kharkov.khpi.tarasenko.bach.web.command.admin;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ua.kharkov.khpi.tarasenko.bach.core.Constant;
import ua.kharkov.khpi.tarasenko.bach.core.EShopException;
import ua.kharkov.khpi.tarasenko.bach.core.db.ConnectionManager;
import ua.kharkov.khpi.tarasenko.bach.core.db.DBUtils;
import ua.kharkov.khpi.tarasenko.bach.core.order.UsersOrdersListBean;
import ua.kharkov.khpi.tarasenko.bach.dao.core.DAOFactory;
import ua.kharkov.khpi.tarasenko.bach.dao.core.order.OrderItemDAO;
import ua.kharkov.khpi.tarasenko.bach.dao.core.order.OrderItemDTO;
import ua.kharkov.khpi.tarasenko.bach.dao.core.order.OrderUserDAO;
import ua.kharkov.khpi.tarasenko.bach.dao.core.order.OrderUserDTO;
import ua.kharkov.khpi.tarasenko.bach.dao.core.user.UserDAO;
import ua.kharkov.khpi.tarasenko.bach.dao.core.user.UserDTO;
import ua.kharkov.khpi.tarasenko.bach.web.command.Command;

public class ShowAllOrdersCommand extends Command {

	private static final String ATTRIBUTE_USERS_ORDERS_BEAN = "usersOrderBean";

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws EShopException {

		Connection conn = null;

		try {
			conn = ConnectionManager.getConnection();
			DAOFactory factory = DAOFactory
					.getDAOFactory(DAOFactory.DAO_TYPE.MYSQL);

			OrderUserDAO orserUserDAO = factory.getOrderUserDAO(conn);
			OrderItemDAO orderItemDAO = factory.getOrderItemDAO(conn);
			UserDAO userDAO = factory.getUserDAO(conn);
			
			ArrayList<Integer> ordersIds = orserUserDAO.findAllOrdersIds();

			ArrayList<UsersOrdersListBean> usersOrders = new ArrayList<UsersOrdersListBean>();
			OrderUserDTO orderUserDTO = null;
			UserDTO userDTO = null;
			ArrayList<OrderItemDTO> itemsInOrder = null;
			for (Integer orderId : ordersIds) {
				itemsInOrder = orderItemDAO.getItemsInOrder(orderId);
				orderUserDTO = orserUserDAO.findOrderById(orderId);
				userDTO = userDAO.findUserById(orderUserDTO.getUserId());
				
				usersOrders.add(createUsersOrdersListBean(orderUserDTO, itemsInOrder, userDTO));
			}
			
			Collections.sort(usersOrders, new CompareOrders());
			request.setAttribute(ATTRIBUTE_USERS_ORDERS_BEAN, usersOrders);

		} catch (EShopException e) {
			DBUtils.close(conn);
			throw e;
		} finally {

			if (conn != null) {
				DBUtils.close(conn);
			}
		}

		return Constant.PAGE_USERS_ORDERS;
	}

	private UsersOrdersListBean createUsersOrdersListBean(
			OrderUserDTO orderUserDTO, ArrayList<OrderItemDTO> itemsInOrder, UserDTO user) {

		UsersOrdersListBean usersOrdersListBean = new UsersOrdersListBean();
		usersOrdersListBean.setUserName(user.getEmail());
		usersOrdersListBean.setOrderId(orderUserDTO.getOrderId());
		usersOrdersListBean.setOrderDate(orderUserDTO.getOrderDate());
		usersOrdersListBean.setStatus(orderUserDTO.getStatus());
		usersOrdersListBean.setItemsInOrder(itemsInOrder);
		
		return usersOrdersListBean;
	}
	
	private class CompareOrders implements Comparator<UsersOrdersListBean> {

		@Override
		public int compare(UsersOrdersListBean o1, UsersOrdersListBean o2) {
			
			if (o1.getUserName().compareTo(o2.getUserName()) == 0) {
				return o1.getOrderDate().compareTo(o2.getOrderDate());
			} else {
				return o1.getUserName().compareTo(o2.getUserName());
			}

		}		
	}
}
